/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.airlogger.proxy
{
	import com.airlogger.utils.ConnectionUtils;
	import com.bourre.plugin.AbstractPlugin;
	
	import flash.net.LocalConnection;
	import com.airlogger.proxy.view.AirLoggerProxyFacade;
	import com.airlogger.log.AirLoggerLogPluginListener;
	import com.airlogger.event.AirLoggerLogEvent;
	import com.airlogger.event.SWFEvent;
	import com.bourre.log.PixlibDebug;

	import flash.events.AsyncErrorEvent;	
	import flash.events.StatusEvent;	
	
import flash.utils.getDefinitionByName;	public class AirLoggerProxy extends AbstractPlugin implements AirLoggerLogPluginListener
	{				public static var ALTERNATE_ID_OUT : String = "";
				
		protected var _lcIn : LocalConnection;
		protected var _lcOut : LocalConnection;
		
		protected var _nID : uint;
		
		public function AirLoggerProxy ()
		{			
			_nID = 0;
		}
		
		public function initConnection() : void
		{
			_lcIn = new LocalConnection();
			_lcIn.allowDomain( "*" );
			_lcIn.client = this;

			connect();

			_lcOut = new LocalConnection( );
			_lcOut.addEventListener( StatusEvent.STATUS, onStatus );
		}

		public function connect () : void
		{
			try
			{
				_lcIn.connect( ConnectionUtils.getInConnectionName() );
			}
			catch ( e : Error )
			{
				
			}
		}
		
		public function close() : void
		{
			_lcIn.close();
		}
		
		public function onDeconnection ( e : SWFEvent ) : void
		{
			var view : AirLoggerProxyFacade = getViewLocator().getView( e.id ) as AirLoggerProxyFacade;
			
			//PixlibDebug.DEBUG( "in AirLoggerProxy.onDeconnection " + e.id + ", " + view );
			
			view.close();
			view.release();
		}
		
		public function requestID ( idOut : String = "" ) : void
		{
			var id : String = _getNewID();
			
			ALTERNATE_ID_OUT = idOut;
			
			//getLogger().debug( "requesting id : " + id + " on " + idOut );
			
			var v : AirLoggerProxyFacade = new AirLoggerProxyFacade ( this, id );

			_lcOut.send( ConnectionUtils.getOutConnectionName( ALTERNATE_ID_OUT ), "setID", id );
		}

		public function idAlreadyUsed ( id : String ) : void
		{
			//getLogger().info( "Id " + id + " is already used by another swf" );
			
			//getModelLocator().getModel( id ).release();
			
			requestID( ALTERNATE_ID_OUT );
		}  
		
		public function mainConnectionAlreadyUsed ( failId : String = "" ) : void
		{
			ALTERNATE_ID_OUT = failId + "_";
			//getLogger().error ( "error on connection, new channel = " + ConnectionUtils.getOutConnectionName( ALTERNATE_ID_OUT ) )
		}

		public function confirmID ( id : String, name : String = null ) : void
		{
			fireOnConnectionEvent( id, name );
		}
		
		public function fireOnConnectionEvent ( id : String, name : String ) : void
		{
			var evt : SWFEvent = new SWFEvent(AirLoggerProxyEventList.onConnectionEVENT, this, id, name );
			firePublicEvent( evt );
		}
		
		public function fireOnDeconnectionEvent ( id : String ) : void
		{
			var evt : SWFEvent = new SWFEvent( AirLoggerProxyEventList.onDeconnectionEVENT, this, id );
			firePublicEvent( evt );
		}
		
		protected function _getNewID () : String
		{
			return ( _nID++ ).toString();
		}
	
		public function onASyncError ( e : AsyncErrorEvent ) : void
		{
			//getLogger().error( e.target + " have emit " + e.error ); 
		}
		
		public function onStatus ( e : StatusEvent ) : void
		{
			//getLogger().debug( "status : " + e );
		}
	}
}